package action.newelifile;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import action.adapter.FavoriteFileAdapter;
import action.adapter.HistoryFileAdapter;
import action.pojo.LocalFilePojo;
import action.util.FavoriteConnectSQLite;
import action.util.Tool;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddressActivity extends Activity{
	Button historyButton,favoriteButton,cancel;
	ListView popupListView;
	TextView txtViewEmpty;
	RelativeLayout clearButton;
	FavoriteConnectSQLite favoriteConnectSQLite;
	int flagviewhistory=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.popup_address);
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

		favoriteConnectSQLite = new FavoriteConnectSQLite(this);
		favoriteConnectSQLite.open();

		initAttribute();

		super.onCreate(savedInstanceState);
	}

	public void initAttribute(){
		historyButton = (Button)findViewById(R.id.historyButton);
		favoriteButton = (Button)findViewById(R.id.favoriteButton);
		popupListView = (ListView)findViewById(R.id.popupListView);
		clearButton = (RelativeLayout)findViewById(R.id.clearButton);
		cancel = (Button)findViewById(R.id.cancel);
		cancel.setText(getString(R.string.cancel));
		txtViewEmpty = (TextView)findViewById(R.id.txtViewEmpty);

		historyButton.setOnClickListener(historyClick());
		favoriteButton.setOnClickListener(favoriteClick());
		popupListView.setOnItemClickListener(onPopupListClick());

		historyButton.performClick();

		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setResult(0);
				finish();
			}
		});
	}

	public OnClickListener clearHistory(){
		OnClickListener onclick = new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(Tool.position==1)Tool.list_history= new ArrayList<LocalFilePojo>();
				finish();
			}
		};
		return onclick;
	}

	//clear favorite Button
	private OnClickListener clearFavorite(){
		OnClickListener action = new OnClickListener() {			
			public void onClick(View v) {
				Tool.list_favorite= new ArrayList<LocalFilePojo>();
				if(Tool.position==1)
					favoriteConnectSQLite.open();
					favoriteConnectSQLite.deleteFavorite();
				finish();
			}
		};
		return action;
	}

	public OnClickListener favoriteClick(){
		OnClickListener onclick = new OnClickListener() {

			@Override
			public void onClick(View v) {
				flagviewhistory=2;
				favoriteButton.setCompoundDrawablesWithIntrinsicBounds(null,null, null, getResources().getDrawable(R.drawable.arrowup) );
				historyButton.setCompoundDrawablesWithIntrinsicBounds(null,null, null , null);
				clearButton.setOnClickListener(clearFavorite());
				ArrayList<LocalFilePojo> temp = new ArrayList<LocalFilePojo>();
				if(Tool.list_favorite!=null)
					temp.addAll(Tool.list_favorite);
				System.out.println(" temp favorite == "  + temp);
				if(temp!=null && temp.size()>0){
					popupListView.setVisibility(ListView.VISIBLE);
					popupListView.setAdapter(new FavoriteFileAdapter(AddressActivity.this, R.layout.history_detail, temp));
					txtViewEmpty.setVisibility(TextView.GONE);
				}
				else{
					popupListView.setVisibility(ListView.GONE);
					txtViewEmpty.setVisibility(TextView.VISIBLE);
					txtViewEmpty.setText("Favorites empty");
				}
			}
		};
		return onclick;
	}

	public OnClickListener historyClick(){
		OnClickListener onclick = new OnClickListener() {

			@Override
			public void onClick(View v) {
				flagviewhistory=1;
				historyButton.setCompoundDrawablesWithIntrinsicBounds(null,null, null, getResources().getDrawable(R.drawable.arrowup) );
				favoriteButton.setCompoundDrawablesWithIntrinsicBounds(null,null, null , null);
				clearButton.setOnClickListener(clearHistory());
				ArrayList<LocalFilePojo> temp = new ArrayList<LocalFilePojo>();
				if(Tool.position==1)
					temp.addAll(Tool.list_history);
				Collections.reverse(temp);
				
				System.out.println(" temp history -- " + temp);
				
				if(temp!=null && temp.size() > 0){
					popupListView.setVisibility(ListView.VISIBLE);
					popupListView.setAdapter(new HistoryFileAdapter(AddressActivity.this, R.layout.history_detail, temp));
					txtViewEmpty.setVisibility(TextView.GONE);
				}
				else{
					popupListView.setVisibility(ListView.GONE);
					txtViewEmpty.setVisibility(TextView.VISIBLE);
					txtViewEmpty.setText("History empty");
				}

			}
		};
		return onclick;
	}

	public OnItemClickListener  onPopupListClick(){
		OnItemClickListener onclick = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try{
					if(flagviewhistory==1){
						//history
						LocalFilePojo object = (LocalFilePojo) parent.getItemAtPosition(position);
						File file = new File(object.getFileItem().getAbsolutePath());
						System.out.println("file path == " + object.getFileItem().getAbsolutePath());
						if(file.isDirectory()){
							if(Tool.position==1){
								Tool.local_path= new ArrayList<String>();
								String[] tem= object.getFileItem().getAbsolutePath().split(File.separator);
								if(tem!=null && tem.length>0){
									for(String s: tem){
										if(!s.equals(null) && !s.equals("")){
											Tool.local_path.add(s);
										}
									}
								}
								Intent intent = new Intent(AddressActivity.this,LocalActivity.class);
								intent.putExtra("paths", "addActi");
								startActivity(intent);
							}
						}else{
							Intent action = new Intent(Intent.ACTION_VIEW);
							if (Tool.checkExtendFile(object.getFileItem().getName(), ".txt")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".csv")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".xml")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".flv")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".3gp")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".avi")) {
								action.setDataAndType(Uri.fromFile(file), "video/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".mp3")) {
								action.setDataAndType(Uri.fromFile(file), "audio/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".doc")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".docx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".ppt")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".pptx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".xls")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".xlsx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".zip")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".rar")) {
								action.setDataAndType(Uri.fromFile(file), "video/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".jpg")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".jpeg")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".png")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".bmp")
									|| Tool.checkExtendFile(object.getFileItem().getName(), ".gif")) {
								action.setDataAndType(Uri.fromFile(file), "image/*");
							} else if (Tool.checkExtendFile(object.getFileItem().getName(), ".apk")) {
								action.setDataAndType(Uri.fromFile(file),
								"application/vnd.android.package-archive");
							} 
							startActivity(action);
						}
					}else if(flagviewhistory==2){
						LocalFilePojo object = (LocalFilePojo) parent.getItemAtPosition(position);
						File file = new File(object.getPath());
						System.out.println("file path == " + object.getPath());
						if(file.isDirectory()){
							if(Tool.position==1){
								Tool.local_path= new ArrayList<String>();
								String[] tem= object.getPath().split(File.separator);
								if(tem!=null && tem.length>0){
									for(String s: tem){
										if(!s.equals(null) && !s.equals("")){
											Tool.local_path.add(s);
										}
									}
								}
								Intent intent = new Intent(AddressActivity.this,LocalActivity.class);
								intent.putExtra("paths", "addActi");
								startActivity(intent);
							}
						}else{
							Intent action = new Intent(Intent.ACTION_VIEW);
							if (Tool.checkExtendFile(object.getName(), ".txt")
									|| Tool.checkExtendFile(object.getName(), ".csv")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getName(), ".xml")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getName(), ".flv")
									|| Tool.checkExtendFile(object.getName(), ".3gp")
									|| Tool.checkExtendFile(object.getName(), ".avi")) {
								action.setDataAndType(Uri.fromFile(file), "video/*");
							} else if (Tool.checkExtendFile(object.getName(), ".mp3")) {
								action.setDataAndType(Uri.fromFile(file), "audio/*");
							} else if (Tool.checkExtendFile(object.getName(), ".doc")
									|| Tool.checkExtendFile(object.getName(), ".docx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getName(), ".ppt")
									|| Tool.checkExtendFile(object.getName(), ".pptx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getName(), ".xls")
									|| Tool.checkExtendFile(object.getName(), ".xlsx")) {
								action.setDataAndType(Uri.fromFile(file), "text/*");
							} else if (Tool.checkExtendFile(object.getName(), ".zip")
									|| Tool.checkExtendFile(object.getName(), ".rar")) {
								action.setDataAndType(Uri.fromFile(file), "video/*");
							} else if (Tool.checkExtendFile(object.getName(), ".jpg")
									|| Tool.checkExtendFile(object.getName(), ".jpeg")
									|| Tool.checkExtendFile(object.getName(), ".png")
									|| Tool.checkExtendFile(object.getName(), ".bmp")
									|| Tool.checkExtendFile(object.getName(), ".gif")) {
								action.setDataAndType(Uri.fromFile(file), "image/*");
							} else if (Tool.checkExtendFile(object.getName(), ".apk")) {
								action.setDataAndType(Uri.fromFile(file),
								"application/vnd.android.package-archive");
							} 
							startActivity(action);
						}
					}

				}catch(Exception ex){
					Toast.makeText(AddressActivity.this, "File is missing ! Please check again !", Toast.LENGTH_SHORT).show();
					ex.printStackTrace();
				}
			}
		};
		return onclick;
	}
}
